using System;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace MT5Trade.Models.MarketData
{
    /// <summary>
    /// 市场数据测试实体类
    /// 严格遵守 generic-rule.md 规则
    /// 对应数据库表：market_data_test
    /// </summary>
    [Table("market_data_test")]
    public class MarketDataTestEntity
    {
        [Key]
        [Column("id")]
        public long Id { get; set; }

        [Required]
        [Column("symbol")]
        [MaxLength(20)]
        public string Symbol { get; set; }

        [Required]
        [Column("time")]
        public DateTime Time { get; set; }

        [Required]
        [Column("open")]
        public decimal Open { get; set; }

        [Required]
        [Column("high")]
        public decimal High { get; set; }

        [Required]
        [Column("low")]
        public decimal Low { get; set; }

        [Required]
        [Column("close")]
        public decimal Close { get; set; }

        [Required]
        [Column("volume")]
        public long Volume { get; set; }

        [Required]
        [Column("interval_type")]
        [MaxLength(20)]
        public string IntervalType { get; set; }

        [Required]
        [Column("data_source")]
        [MaxLength(50)]
        public string DataSource { get; set; }

        [Column("created_at")]
        public DateTime CreatedAt { get; set; }

        /// <summary>
        /// 转换为MarketTick对象
        /// </summary>
        public MarketTick ToMarketTick()
        {
            return new MarketTick
            {
                Symbol = Symbol,
                Time = Time,
                Open = Open,
                High = High,
                Low = Low,
                Close = Close,
                Volume = Volume
            };
        }

        /// <summary>
        /// 从MarketTick创建实体
        /// </summary>
        public static MarketDataTestEntity FromMarketTick(MarketTick tick, string intervalType, string dataSource)
        {
            return new MarketDataTestEntity
            {
                Symbol = tick.Symbol,
                Time = tick.Time,
                Open = tick.Open,
                High = tick.High,
                Low = tick.Low,
                Close = tick.Close,
                Volume = tick.Volume,
                IntervalType = intervalType,
                DataSource = dataSource,
                CreatedAt = DateTime.UtcNow
            };
        }
    }
}